<template>
    <div class="app-container">
       <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="90px">
          <el-form-item label="群组名称：" prop="groupName">
             <el-input
                v-model="queryParams.groupName"
                placeholder="请输入"
                clearable
                style="width: 240px"
                @keyup.enter="handleQuery"
             />
          </el-form-item>
          <el-form-item label="群组ID：" prop="groupID">
             <el-input
                v-model="queryParams.groupID"
                placeholder="请输入"
                clearable
                style="width: 240px"
                @keyup.enter="handleQuery"
             />
          </el-form-item>
          <el-form-item>
             <el-button icon="Refresh" @click="resetQuery">重置</el-button>
             <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
          </el-form-item>
       </el-form>
       <el-row :gutter="10" class="mb8"> 
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" size="mini" @click="handleAdd">新建群组</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
       </el-row>
       <!-- 表格数据 -->
       <el-table v-loading="loading" :data="roleList">
          <el-table-column label="" prop="id" width="100" >
            <template #default="scope">
                <span class="idx"  :style="{ backgroundColor: scope.$index < 3 ? 'black' : 'gray' }">
                  {{scope.$index + 1}}
                </span>
            </template>
          </el-table-column>
          <el-table-column label="群组头像" :show-overflow-tooltip="true" width="100">
            <template #default="scope">
              <span class="avatar" v-if="scope.row.groupInfo.faceURL === ''">
                  {{scope.row.groupInfo.groupName[0]}}
              </span>
              <div v-else>
                <img :src="scope.row.groupInfo.faceURL" alt="light" />
              </div>
            </template>
          </el-table-column>
          <el-table-column label="群组名称" :show-overflow-tooltip="true" width="100">
            <template #default="scope">
                <span>
                  {{scope.row.groupInfo.groupName}}
                </span>
            </template>
          </el-table-column>
          <el-table-column label="群组ID">
            <template #default="scope">
                <span>
                  {{scope.row.groupInfo.groupID}}
                </span>
            </template>
          </el-table-column>
          <el-table-column label="群人数" width="100">
            <template #default="scope">
                <span>
                  {{scope.row.groupInfo.memberCount}}
                </span>
            </template>
          </el-table-column>
          <el-table-column label="群主ID" prop="groupOwnerUserID" width="100"/>
          <el-table-column label="创建时间" align="center" prop="createTime">
            <template #default="scope">
              <span>{{ parseTime(scope.row.groupInfo.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
             <template #default="scope">
                 <el-button link type="primary"  @click="handleMembers(scope.row)">群成员</el-button>
                 <el-button link type="primary"  @click="handleSet(scope.row)">群设置</el-button>
                 <el-popconfirm   
                    confirmButtonText="确定"
                    cancelButtonText="取消"
                    title="确认要开启全体禁言吗？"
                    @confirm="handleMute(scope.row)"
                 >
                    <template #reference>
                        <el-button link type="primary">全体禁言</el-button>
                    </template>
                 </el-popconfirm>
                 <el-popconfirm   
                    confirmButtonText="确定"
                    cancelButtonText="取消"
                    title="确认要解散该群组吗？"
                    @confirm="handleDismiss(scope.row)"
                 >
                    <template #reference>
                        <el-button link type="primary">解散</el-button>
                    </template>
                 </el-popconfirm>
                 
             </template>
          </el-table-column>
       </el-table>
 
       <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="pageObj.pageNumber"
          v-model:limit="pageObj.showNumber"
          @pagination="getList"
       />
       <el-drawer :title="title" v-model="open" direction="rtl" :before-close="handleClose" destroy-on-close>
          <div class="upload_c">
            <div class="upload">
                <el-icon :size="20"><plus /></el-icon>
            </div>
            <div class="upload_text">点击上传</div>
          </div>
          
          <el-form ref="roleRef" :model="form" :rules="rules" label-width="100px">
             <el-form-item label="群组名称" prop="groupName">
                <el-input v-model="form.groupName" placeholder="请输入" />
             </el-form-item>
             <el-form-item label="群组ID" prop="groupId">
                <el-input v-model="form.groupId" placeholder="请输入" />
             </el-form-item>
             <el-form-item label="群验证" prop="validator">
                <el-select v-model="form.validator" placeholder="请选择">
                    <el-option
                        v-for="item in validatorOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    >
                    </el-option>
                </el-select>
             </el-form-item>
             <el-form-item label="添加好友" prop="validator">
                <el-select v-model="form.validator" placeholder="请选择">
                    <el-option
                        v-for="item in AddOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    >
                    </el-option>
                </el-select>
             </el-form-item>
             <el-form-item label="查看资料" prop="validator">
                <el-select v-model="form.validator" placeholder="请选择">
                    <el-option
                        v-for="item in AddOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    >
                    </el-option>
                </el-select>
             </el-form-item>
             <el-form-item label="群公告" prop="announcement">
                <el-input type="textarea" :rows="2" v-model="form.announcement" placeholder="请输入群公告" />
             </el-form-item>
             <el-form-item label="群简介" prop="introduction">
                <el-input  type="textarea" :rows="2" v-model="form.introduction" placeholder="请输入群简介" />
             </el-form-item>
          </el-form>
          <template #footer>
             <div class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
             </div>
          </template>
       </el-drawer>
    </div>
 </template>
 
 <script setup name="Role">
 import { getGroupList } from "@/api/im/group"; 
 import { ref } from "vue";
 
 const router = useRouter();
 const { proxy } = getCurrentInstance();
 const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
 
 const roleList = ref([]);
 const loading = ref(true);
 const showSearch = ref(true);
 const ids = ref([]);
 const single = ref(true);
 const multiple = ref(true);
 const total = ref(0);
 const title = ref("");
 const type = ref('add')
 const dateRange = ref([]);
 const open = ref(false);
 const validatorOptions = [
    {value: '1', label: '需要发送验证消息'},
    {value: '2', label: '允许所有人加群'},
    {value: '3', label: '群成员邀请无需验证'}
 ]
 const AddOptions = [
    {value: '1', label: '允许群内添加好友'},
    {value: '2', label: '不允许群内添加好友'}
 ]
 
 const data = reactive({
   form: {},
   queryParams: {
     current: 1,
     pageSize: 10,
     groupName: undefined,
     groupID: '',
   },
   pageObj:{
    pageNumber: 1,
    showNumber: 10,
   },
   rules: {
     groupName: [{ required: true, message: "群组名称不能为空", trigger: "blur" }],
   },
 });
 
 const { queryParams, form, rules, pageObj } = toRefs(data);
 
 /** 查询角色列表 */
 function getList() {
   loading.value = true;
   const parmas = {
    pagination: data.pageObj,
    ...data.queryParams
   }
   getGroupList(parmas).then(response => {
     roleList.value = response.data.groups;
     total.value = response.data.total;
     loading.value = false;
   });
 }
 
 /** 搜索按钮操作 */
 function handleQuery() {
   queryParams.value.pageNum = 1;
   getList();
 }
 
 /** 重置按钮操作 */
 function resetQuery() {
   dateRange.value = [];
   proxy.resetForm("queryRef");
   handleQuery();
 }
 /** 群成员 */  
 function handleMembers(row){
    router.push({ name: 'groupMember', params: {groupID: row.groupInfo.groupID} })
 }
 /**群设置 */
 function handleSet (){
    open.value = true;
 }
 getList();
 </script>
<style scoped>
.upload_c{
    margin-bottom: 10px;
}
.upload_text{
    text-align: center;
    margin: 0 auto;
    margin-top: 10px;
    color: #1890ff;
    font-size: 14px;
}
.upload{
    width: 53px;
    height: 53px;
    border-radius: 10px;
    padding: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f5f5f5;
    margin: 0 auto;
}
</style>
 